<?php

/**
 * @Author: kendy
 * @Date:   2020-03-05 10:31:48
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-01 15:11:42
 */
namespace app\api\model; 
use think\Model;
use think\Db;  
class bargain extends Model
{
    /**
     * [productlist 砍价商品列表]
     * @Author   kendy
     * @DateTime 2020-03-05T10:33:05+0800
     * @return   [type]                   [description]
     */
	public function productlist($where,$bargainingStartId,$uid,$field,$page){  
       if(!empty($bargainingStartId)){ 
         	//正在进行当中的
	       $bargainGoods = Db::name("bargain_start") 
	       ->where(['id'=>array('in',$bargainingStartId)]) 
	       ->select(); 
	       $goodsIds = [];
	        if(count($bargainGoods)>0){ 
		       foreach ($bargainGoods as $key => $value) {
		         $goodsIds[] = $value['goods_id'];
		       }
		     } 

		  $where['id'] =   array('not in',$goodsIds); 
       } 
       $bargainGoods = Db::name("bargain_goods")
       ->field($field)
       ->where($where)  
       ->order('create_time desc')
       ->limit(($page-1)*config('page_size'),config('page_size'))
       ->select();

       
       if(count($bargainGoods)>0) {
          foreach ($bargainGoods as $key => $value) {
                $bargainGoods[$key]['is_free'] = $value['bargain_money']>0?0:1;
                unset($bargainGoods[$key]['bargain_money']);
              }
       	 
        	return $bargainGoods;
       }
       else return [];
       
	}

   /**
    * [bargaining 查看用户的砍价信息]
    * @Author   kendy
    * @DateTime 2020-03-05T13:57:31+0800
    * @param    [type]                   $uid [description]
    * @return   [type]                        [description]
    */
   public function bargaining($where){ 
        //在砍价发起的24h之内，在发起表里，查看is_have是否是正在砍价0
       $bargainingList = Db::name('bargain_start')
       ->field("bs.id as start_id,bg.id as goods_id,bg.goods_image,bg.goods_name,bg.goods_money,bg.bargain_money as low_money,bs.bargain_money,bs.create_time")
       ->alias("bs")
       ->join('bargain_goods bg','bg.id=bs.goods_id','inner') 
       ->where($where)
       ->order('bs.id desc')
       ->select(); 
       if(count($bargainingList)>0){ 
       	foreach ($bargainingList as $key => $value) {   
            //是否是免费产品
             $bargainingList[$key]['is_free'] = $value['low_money']>0?0:1; 
            if($value['low_money'] == 0){

            	/*$bargainingList[$key]['diff_money'] = $value['goods_money'] - $value['bargain_money'];*/
              $bargainingList[$key]['diff_money'] =  (string)(bcsub($value['goods_money'] ,$value['bargain_money'],2));
            	$bargainingList[$key]['progress'] = round(($value['bargain_money'] / $value['goods_money'])*100,2).'%';  

            }else{

                /*$bargainingList[$key]['diff_money'] = ($value['goods_money'] - $value['low_money']) - $value['bargain_money'];*/

                $needBargainMoney = bcsub($value['goods_money'] ,$value['low_money'],2);
                $bargainingList[$key]['diff_money'] = (string)(bcsub($needBargainMoney , $value['bargain_money'],2));   


                $bargainingList[$key]['progress'] = round($value['bargain_money'] / ($value['goods_money'] - $value['low_money'])*100,2).'%';
            }
             //截止时间， 
             $bargainingList[$key]['end_time'] =  strtotime($value['create_time']) + 24 * 60 * 60;

             unset($bargainingList[$key]['low_money']);
             unset($bargainingList[$key]['create_time']);
             unset($bargainingList[$key]['goods_money']);
       	}

       	return $bargainingList;
       }
       else return [];   
   }

    public function bargainUserList($start_id){
        $result['bargain_users'] = [];
		$result['bargain_users'] = Db::name('bargain_list')
		->alias("bl")
		->field("bargain_money,nickname,user_icon,bl.is_read")
		->join('user u','u.uid = bl.help_id','inner')
		->where('start_id',$start_id)
		->order("bl.create_time desc")
		->select();
			$result['help_bargain_count'] = 0;
			$result['bargain_total_money'] = 0; 
		if(count($result['bargain_users'])>0){ 
			$result['help_bargain_count'] = count($result['bargain_users']);
			$bargain_total_money = Db::name('bargain_list')
			->where('start_id',$start_id)
			->sum("bargain_money"); 
      $result['bargain_total_money'] = (string)$bargain_total_money;
          return $result;
		}
        return $result;
    }

    /**
    * [bargaining 查看用户的砍价详情信息]
    * @Author   kendy
    * @DateTime 2020-03-06T10:57:31+0800
    * @param    [type]                   $uid [description]
    * @return   [type]                        [description]
    */
   public function bargainInfo($where){
        //在砍价发起的24h之内，在发起表里，查看is_have是否是正在砍价0
       $bargaining = Db::name('bargain_start')
       ->field("bs.id as start_id,bg.id as goods_id,bg.goods_image,bg.goods_name,bg.goods_money,bg.bargain_money as low_money,bs.bargain_money,is_have,bs.create_time")
       ->alias("bs")
       ->join('bargain_goods bg','bg.id=bs.goods_id','inner') 
       ->where($where)
       ->find();
       if(!empty($bargaining)){ 
       //	foreach ($bargainingList as $key => $value) { 

            //是否是免费产品
             $bargaining['is_free'] = $bargaining['low_money']>0?0:1; 
            if($bargaining['low_money'] == 0){

            	$bargaining['diff_money'] = (string) bcsub($bargaining['goods_money'] ,$bargaining['bargain_money'],2);

            	$bargaining['progress'] = round(($bargaining['bargain_money'] / $bargaining['goods_money'])*100,2).'%';  

            }else{
                $needBargainMoney = bcsub($bargaining['goods_money'] ,$bargaining['low_money'],2);
                $bargaining['diff_money'] = (string)(bcsub($needBargainMoney , $bargaining['bargain_money'],2));   
                $bargaining['progress'] = round($bargaining['bargain_money'] / ($bargaining['goods_money'] - $bargaining['low_money'])*100,2).'%';
            }
             //截止时间，  
             $bargaining['end_time'] =  strtotime($bargaining['create_time']) + 24 * 60 * 60;
             unset($bargaining['create_time']); 
     //}
 
       	return $bargaining;
       }
       else return [];   
   }
     
}